<template>
  <div class="gw-more">
    <div v-show="show" ref="meta" class="meta"><slot /></div>
    <div class="control" @click="handleMore">
      <i :class="ctrlIcon" />{{ ctrlText }}
    </div>
  </div>
</template>

<script>
/**
 * 是否显示更多内容
 * @params {Boolean} show: 首次将在是否将更多内容直接显示
 * @params {Function} collapse: 当开关隐藏内容后回调函数
 * @example
 *  <GwMore show @collapse="callback">//...</GwMore>
 */

import './raf' // polyfill
import { slideDown, slideUp, slideStop } from 'slide-anim'

export default {
  name: 'GwMore',
  props: {
    show: Boolean
  },
  data() {
    return {
      internalMore: this.show
    }
  },
  computed: {
    ctrlIcon() {
      return this.internalMore ? 'el-icon-caret-top' : 'el-icon-caret-bottom'
    },
    ctrlText() {
      return this.internalMore ? '隐藏更多' : '显示更多'
    }
  },
  methods: {
    handleMore() {
      const meta = this.$refs.meta
      this.internalMore = !this.internalMore
      if (this.internalMore) {
        slideStop(meta)
        slideDown(meta)
      } else {
        slideStop(meta)
        slideUp(meta)
      }
      this.$emit('collapsed', this.internalMore)
    }
  }
}
</script>

<style lang="scss">
@import '~@style';

.gw-more {
  .control {
    position: relative;
    box-sizing: border-box;
    height: 44px;
    margin-top: -1px;
    color: $colorReadonly;
    font-size: 14px;
    text-align: center;
    background-color: $ui-section-bg;
    border-radius: 4px;
    cursor: pointer;
    user-select: none;

    &:hover {
      color: $blue;
      background-color: rgba(0,0,0,.04);
    }

    i {
      margin-right: 10px;
      font-size: 16px;
      line-height: 44px;
      transition: .3s;
    }
  }

  .ui-dark-mode & .control {
    background-color: $ui-section-bg-reverse;

    &:hover {
      color: darken($blue, $ui-color-darken-percent);
      background-color: $ui-border-color-reverse;
    }
  }
}

</style>
